package com.definity.toolkit.ui.widget;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.border.Border;

import com.definity.toolkit.ui.Align;
import com.definity.toolkit.ui.Body;
import com.definity.toolkit.ui.action.ActionExecutor;

public class Label extends JLabel implements WidgetExecutor<Label> {

	private static final long serialVersionUID = 4709082764316042847L;
	
	public Label() {

	}

	public Label(Icon image) {
		super(image);
	}

	public Label(String text) {
		super(text);
	}

	@Override
	public String id() {
		return getName();
	}

	@Override
	public Label id(String id) {
		setName(id);
		return this;
	}

	@Override
	public Label properties(Body<Label> body) {
		body.content(this);
		return this;
	}

	@Override
	public Color background() {
		return getBackground();
	}

	@Override
	public Label background(Color background) {
		setBackground(background);
		return this;
	}

	@Override
	public Color foreground() {
		return getForeground();
	}

	@Override
	public Label foreground(Color foreground) {
		setForeground(foreground);
		return this;
	}

	@Override
	public Label size(int width, int height) {
		setPreferredSize(new Dimension(width, height));
		return this;
	}

	@Override
	public Font font() {
		return getFont();
	}

	@Override
	public Label font(Font font) {
		setFont(font);
		return this;
	}
	
	public String text() {
		return getText();
	}
	
	public Label text(String text) {
		setText(text);
		return this;
	}
	
	public String tooltip() {
		return getToolTipText();
	}
	
	public Label tooltip(String tooltip) {
		setToolTipText(tooltip);
		return this;
	}

	public Icon icon() {
		return getIcon();
	}
	
	public Label icon(Icon icon) {
		setIcon(icon);
		return this;
	}

	public Align horizontalAlignment() {
		switch (getHorizontalAlignment()) {
		case LEFT:
			return Align.LEFT;
		case CENTER:
			return Align.CENTER;
		case RIGHT:
			return Align.RIGHT;
		}
		return Align.NONE;
	}
	
	public Label horizontalAlignment(Align align) {
		switch (align) {
		case LEFT:
			setHorizontalAlignment(LEFT);
			break;
		case CENTER:
			setHorizontalAlignment(CENTER);
			break;
		case RIGHT:
			setHorizontalAlignment(RIGHT);
			break;
		}
		return this;
	}

	public Border border() {
		return getBorder();
	}
	
	public Label border(Border border) {
		setBorder(border);
		return this;
	}

	@Override
	public Label action(final ActionExecutor executor) {
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent event) {
				if (event.getButton() == MouseEvent.BUTTON1 && event.getClickCount() == 1) {
					executor.execute(Label.this);
				}
			}
		});
		return this;
	}
	
}
